package com.yang.config;

import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;

/**
 * @author: 杨庆
 * @since: 2022.9.10 21:47
 * @description:
 */
@Component
@ConfigurationProperties(prefix = "aliyun")
@Data
public class AliyunCompoent {
    private String accessKeyId;
    private String accessKeySecret;
    private Oss oss;
    private Sms sms;
    public OSSClient getOssClient(){
        OSSClient ossClient = new OSSClient(oss.getEndPoint(), accessKeyId, accessKeySecret);
        return ossClient;
    }
    public PutObjectResult fileUpload(String fileName, File file){
        OSSClient ossClient = getOssClient();
        PutObjectResult putObjectResult = ossClient.putObject(oss.getBucket(), fileName, file);
        return putObjectResult;
    }
    public PutObjectResult fileUpload(String fileName, InputStream inputStream){
        OSSClient ossClient = getOssClient();
        PutObjectResult putObjectResult = ossClient.putObject(oss.getBucket(), fileName, inputStream);
        return putObjectResult;
    }

    private static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }
    public void sendMessages(String phone) throws Exception {
        com.aliyun.dysmsapi20170525.Client client = AliyunCompoent.createClient(accessKeyId, accessKeySecret);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName(sms.getSignName())
                .setTemplateCode(sms.getTemplateCode())
                .setPhoneNumbers(phone)
                .setTemplateParam("{\"code\":\"789564\"}");
        RuntimeOptions runtime = new RuntimeOptions();
        SendSmsResponse resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
    }

}
